package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.mapper.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:ahang
 * @create:2022-09-02 21:05
 * @Description:
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;


    @Override
    public void save(Map<String, Object> map) {
        //1.map -->  Department
        String jsonString = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(jsonString, Department.class);
        //2.根据hoscode和depcode 查询科室信息
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        Department existDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        //3.判断是添加还是修改
        if(existDepartment==null){
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        }else{
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        }

    }

    /**
     *条件 + 分页
     */
    @Override
    public Page<Department> selectPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");

        //mongodb 中0代表第一页
        PageRequest pageRequest = PageRequest.of(page-1,limit,sort);
        //将departmentQueryVo转换成department
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);

        ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreCase(true).withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
        Example<Department> example = (Example<Department>) Example.of(department, matcher);

        Page<Department> all = departmentRepository.findAll(example, pageRequest);


        return all;
    }

    /**
     * 删除
     */
    @Override
    public void remove(String hoscode, String depcode) {

        //根据hoscode和depcode查询数据
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        //删除
        departmentRepository.deleteById(department.getId());
    }

    /**
     * 获取科室树
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //1.new bigDeptList来存放大科室集合
        List<DepartmentVo> bigDeptList = new ArrayList<>();
        //2.根据医院编号查询所有的小科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> list = departmentRepository.findAll(example);
        //3.查询到的小科室根据大科室编号分组 string-->大科室编号  list<Department>-->大科室对应的小科室
        Map<String, List<Department>> collect = list.stream().collect(
                Collectors.groupingBy(Department::getBigcode)
        );
        //4.遍历分组之后的map集合
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            //4.1 获取map中的key和value
            String key = entry.getKey();
            List<Department> value = entry.getValue();
            //4.2 转换泛型,将department转换成DepartmentQueryVo
            List<DepartmentVo> childern = new ArrayList<>();
            value.forEach(item->{
                DepartmentVo small = new DepartmentVo();
                small.setDepcode(item.getDepcode());
                small.setDepname(item.getDepname());
                small.setChildren(null);
                childern.add(small);
            });
            //4.3 封装一个大科室集合
            DepartmentVo big = new DepartmentVo();
            big.setDepcode(key);
            big.setDepname(value.get(0).getBigname());
            big.setChildren(childern);

            bigDeptList.add(big);
        }

        return bigDeptList;
    }

    //根据医院编号 科室编号获取科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!= null){
            return department.getDepname();
        }
        return null;
    }

    /**
     * 根据医院编号和部门编号查询科室
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public Department getDepartment(String hoscode, String depcode) {

        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
